import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 异常练习2（完）
 */

class Person {
    private String name;
    private int gender;
    private int age;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class Test {
    //try-catch-fianlly使用方法二
    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            int x = scanner.nextInt();
            System.out.println(x);
        } catch (InputMismatchException e) {
            e.printStackTrace();
            System.out.println("输入的数据不匹配！");
        } finally {
            System.out.println("finally里的代码被执行！");
        }
    }
    //try-catch-fianlly使用方法一
    public static void main7(String[] args) {
        Scanner scanner  = new Scanner(System.in);
        try{
            int x = scanner.nextInt();
            System.out.println(x);
        }catch(InputMismatchException e) {
            e.printStackTrace();
            System.out.println("输入的数据不匹配！");
        }finally {
            System.out.println("finally里的代码被执行！");
            scanner.close();
        }
    }

    //利用 throw 主动抛出异常
    public static void test1(int x) {
        try {
            if(x == 1) {
                throw new RuntimeException();
            }
        } catch (RuntimeException e){
            e.printStackTrace();
        }
    }

    public static void main6(String[] args) {
        test1(1);
        System.out.println("程序继续执行");
    }

    //当异常之间具有父子关系时
    public static void main5(String[] args) {
        try {
            System.out.println(10 / 0);
        }catch(ArithmeticException e) {
            //快速地定位异常出现的位置
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("程序继续执行！");
    }

    //try-catch测试
    public static void main4(String[] args) {
        try {
            System.out.println(10 / 0);
        }catch(ArithmeticException |
               ArrayIndexOutOfBoundsException |
               NullPointerException e) {
            //快速地定位异常出现的位置
            e.printStackTrace();
        }
        System.out.println("程序继续执行！");
    }

    public static void main3(String[] args) {
        try {
            System.out.println(10 / 0);
        }catch(ArithmeticException e) {
            //快速地定位异常出现的位置
            e.printStackTrace();
            System.out.println("算术异常！");
        }catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
            System.out.println("数组越界异常！");
        }catch (NullPointerException e) {
            e.printStackTrace();
            System.out.println("空指针异常！");
        }
        System.out.println("程序继续执行！");
    }

    //编译时异常
    public static void main2(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        Person person1 = (Person) person.clone();
    }

    public static void main1(String[] args) {
        //算术异常
        /*System.out.println(10 / 0);*/
        //数组越界异常
        /*int[] arr = new int[]{1,2,3};
        System.out.println(arr[4]);*/
        //空指针异常
        String s = null;
        System.out.println(s.indexOf(2));
    }
}
